// Load requirejs
var requirejs = require('requirejs');

// Configure requirejs
requirejs.config(
{
    //Use node's special variable __dirname to
    //get the directory containing this file.
    //Useful if building a library that will
    //be used in node but does not require the
    //use of node outside
    baseUrl: __dirname,

    //Pass the top-level main.js/index.js require
    //function to requirejs so that node modules
    //are loaded relative to the top-level JS file.
    nodeRequire: require,

    // Heres how path mapping works: https://github.com/jrburke/requirejs/issues/479
    map:{
                '*' : 
                {
                    "Messages": "../Messages",
                    "Common": "../Common"
                }
            }
});

var Resolver = {};

// Main entry point
requirejs(
    ["ConnectionManager", 
     "Configuration",
     "WebServer"], 
     function(ConnectionManager, Configuration, WebServer) 
    {
        var fs = require('fs');
        var http = require('http')
        var socketio = require('socket.io');
        var express  = require('express');
        var app = express();
        var config = new Configuration().readConfig();        

        var server = http.createServer(app);
        server.listen(config.port, "0.0.0.0");                
        var io = socketio.listen(server);
        var connMgr = new ConnectionManager(io);
        io.on('connection', connMgr.handleSocketEvents);        

        var path = require("path");
        config.wwwRoot = path.join(__dirname,  config.wwwRoot);
        var webserver = new WebServer(io, app, config);
    }
);